import 'package:flutter/material.dart';
import 'package:flutter_fun_app/page/loginPage.dart';
import 'package:flutter_fun_app/utils/setting.dart';
import 'package:flutter_fun_app/model/userModel.dart';
import 'package:flutter_common/config/config.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter_fun_app/page/langSelectPage.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';
import 'package:flutter_common/widget/funAppBar.dart';

class UserPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return UserState();
  }
}

class UserState extends State<UserPage> {
  String userName;
  String img;
  bool isLogin = false;
  List<String> titles = [
    Ids.user_center_phone,
    Ids.user_center_sign,
    Ids.user_center_remarks,
    Ids.user_center_register_time,
    Ids.user_center_register_qrcode,
    Ids.user_center_register_lang
  ];
  List<String> contents = List();
  List<IconData> icons = [
    Icons.phone,
    Icons.edit,
    Icons.insert_drive_file,
    Icons.date_range,
    Icons.apps,
    Icons.language
  ];
  String barcode = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((callBack) {
      initUserInfo();
    });
  }

  void initUserInfo() async {
    bool isLogin = Setting.isLogin();
    print(Setting.getLang());
    if (isLogin) {
      UserInfo model = Setting.getUserInfo();
      setState(() {
        this.isLogin = isLogin;
        userName = model.name;
        img = model.img;
        contents
          ..add(model.phone)
          ..add(model.text)
          ..add(model.other)
          ..add(model.createTime)
          ..add(Ids.user_center_click_to_check)
          ..add('');
      });
    } else {
      setState(() {
        this.isLogin = isLogin;
        userName = Ids.user_center_login_register;
        contents.clear();
      });
    }
  }

  Widget SelectView(IconData icon, String text, String id) {
    return new PopupMenuItem<String>(
        value: id,
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new Icon(icon, color: Config.titleBarColor),
            new Text(
              text,
              style: TextStyle(color: Config.textGrey),
            ),
          ],
        ));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: FunAppBar(
        title: IntlUtil.getString(context, Ids.title_user),
        actions: <Widget>[
          isLogin
              ? IconButton(
                  icon: Icon(
                    isLogin ? Icons.exit_to_app : Icons.more_horiz,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    Setting.setLogin(false);
                    initUserInfo();
                  },
                )
              : new PopupMenuButton<String>(
                  itemBuilder: (BuildContext context) =>
                      <PopupMenuItem<String>>[
                    this.SelectView(
                        Icons.group,
                        IntlUtil.getString(context, Ids.user_center_login),
                        'A'),
                    this.SelectView(
                        Icons.group_add,
                        IntlUtil.getString(context, Ids.user_center_register),
                        'B'),
                  ],
                  onSelected: (String action) {
                    // 点击选项的时候
                    switch (action) {
                      case 'A':
                        goLoginPage();
                        break;
                      case 'B':
                        break;
                    }
                  },
                ),
          IconButton(
              icon: Icon(
                Icons.crop_free,
                color: Colors.white,
              ),
              onPressed: () {})
        ],
      ),
      body: ListView(
        children: <Widget>[
          Container(
            color: Colors.deepOrange,
            height: 160,
            child: Container(
                child: GestureDetector(
              child: Row(
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(left: 30, right: 15),
                    child: isLogin
                        ? ClipOval(
                            child: Image.network(img, height: 60, width: 60),
                          )
                        : IconButton(
                            icon: Icon(
                              Icons.supervised_user_circle,
                              color: Colors.white,
                            ),
                            iconSize: 60,
                          ),
                  ),
                  Text(
                    isLogin ? userName : IntlUtil.getString(context, userName),
                    style: TextStyle(color: Colors.white, fontSize: 14),
                  )
                ],
              ),
              onTap: () {
                if (isLogin) return;
                goLoginPage();
              },
            )),
          ),
          ListView.builder(
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: titles.length,
              itemBuilder: _itemBuilder),
        ],
      ),
    );
  }

  void goLoginPage() {
    Navigator.push<bool>(
            context, new MaterialPageRoute(builder: (context) => LoginPage()))
        .then((bool isLogin) {
      initUserInfo();
    });
  }

  Widget _itemBuilder(BuildContext context, int index) {
    String title = IntlUtil.getString(context, titles[index]);
    IconData icon = icons[index];
    String content;
    if (contents.length > 0) {
      if (index == titles.length - 2) {
        content = IntlUtil.getString(context, contents[index]);
      } else {
        content = contents[index];
      }
    } else {
      content = '';
    }
    if (index == titles.length - 1) {
      content = Setting.getLang() == Setting.ZH ? '中文' : 'English';
    }
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(15),
            child: Row(
              children: <Widget>[
                Icon(
                  icon,
                  color: Config.textGrey,
                ),
                Padding(padding: EdgeInsets.only(right: 10)),
                Text(
                  title,
                  style: TextStyle(color: Config.textGrey),
                ),
                Expanded(
                  child: GestureDetector(
                    child: Text(content,
                        textAlign: TextAlign.right,
                        style: TextStyle(color: Config.textGrey)),
                    onTap: () {
                      if (index == titles.length - 2 && isLogin) {
                        showQRCodeDialog();
                      }
                      if (index == titles.length - 1) {
                        Navigator.push(
                            context,
                            new MaterialPageRoute(
                                builder: (context) => LangSelectPage()));
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
          Divider(height: 2),
        ],
      ),
    );
  }

  Future<void> showQRCodeDialog() async {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return WillPopScope(
          child: qrCodeDialog(userName),
        );
      },
    );
  }
}

class qrCodeDialog extends Dialog {
  String content;

  qrCodeDialog(this.content);

  @override
  Widget build(BuildContext context) {
    return new Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: new Center(
        //保证控件居中效果
        child: new SizedBox(
            height: 250,
            child: Column(
              children: <Widget>[
                Container(
                  child: QrImage(
                    data: content,
                    size: 200.0,
                  ),
                  color: Colors.white,
                ),
                GestureDetector(
                  child: Padding(
                      child: Icon(
                        Icons.highlight_off,
                        color: Colors.white,
                        size: 30,
                      ),
                      padding: EdgeInsets.only(top: 10)),
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            )),
      ),
    );
  }
}
